<template>
  <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="58px">
        <el-form-item label="小区" prop="areaName">
          <el-select :filter-method="filterBindMethod" @change="getGroupBind(queryParams.areaName)" allow-create
                     clearable
                     default-first-option
                     filterable placeholder="请输入小区" size="small" v-model="queryParams.areaName">
            <el-option :disabled="item.disabled" :key="index" :label="item.areaName"
                       :value="item.areaName" v-for="(item, index) in areaBindIdOptions"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼宇" prop="groupName">
          <el-select @change="getUnitBind(queryParams.areaName,queryParams.groupName)" clearable
                     placeholder="请输入楼宇"
                     size="small"
                     v-model="queryParams.groupName">
            <el-option :disabled="item.disabled" :key="index" :label="item.groupName"
                       :value="item.groupName" v-for="(item, index) in groupBindIdOptions"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单元" prop="unitName">
          <el-select @change="getDoorBind(queryParams.areaName,queryParams.groupName,queryParams.unitName)" clearable
                     placeholder="请输入楼宇" size="small" v-model="queryParams.unitName">
            <el-option :disabled="item.disabled" :key="index" :label="item.unitName"
                       :value="item.unitName" v-for="(item, index) in unitBindNameOptions"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="户" prop="doorName">
          <el-select clearable placeholder="请输入楼宇" size="small" v-model="queryParams.doorName">
            <el-option :disabled="item.disabled" :key="index" :label="item.doorName"
                       :value="item.doorName" v-for="(item, index) in doorBindNameOptions"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input
            v-model="queryParams.userName" clearable
            placeholder="请输入姓名"
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            :disabled="single"
            @click="updateBind"
            size="mini"
          >绑定
          </el-button>
          <el-button
            type="success"
            plain
            icon="el-icon-plus"
            @click="insertBind"
            size="mini"
          >新增
          </el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="leftData"
                @row-click="onSelectOp"
                @select-all="onSelectAll"
                @selection-change="selectItem"
                highlight-current-row
                ref="leftData">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="地址" align="center" prop="doorAddr" width="200"/>
        <el-table-column label="房屋编号" align="center" prop="houseId" width="150"/>
        <el-table-column label="姓名" align="center" prop="userName"/>
        <el-table-column label="电话" align="center" prop="tel"/>
        <el-table-column :formatter="numberFormat" label="面积" align="center" prop="space"/>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getLeftList"
      />

      <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="小区" prop="areaName">
            <el-select v-model="form.areaName" :style="{width: '100%'}" :filter-method="filterBindMethod" allow-create
                       clearable
                       default-first-option
                       filterable placeholder="请输入小区" size="small" @change="getGroupBind(form.areaName)">
              <el-option v-for="(item, index) in areaBindIdOptions" :key="index" :disabled="item.disabled"
                         :label="item.areaName" :value="item.areaName"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="楼宇" prop="groupName">
            <el-select v-model="form.groupName" :style="{width: '100%'}" clearable placeholder="请输入楼宇"
                       @change="getUnitBind(form.areaName,form.groupName)">
              <el-option v-for="(item, index) in groupBindIdOptions" :key="index" :disabled="item.disabled"
                         :label="item.groupName" :value="item.groupName"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="单元" prop="unitName">
            <el-select v-model="form.unitName" :style="{width: '100%'}" clearable placeholder="请输入单元">
              <el-option v-for="(item, index) in unitBindNameOptions" :key="index" :disabled="item.disabled"
                         :label="item.unitName" :value="item.unitName"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="户" prop="doorName">
            <el-select clearable placeholder="请输入楼宇" size="small" v-model="form.doorName" :style="{width: '100%'}">
              <el-option :disabled="item.disabled" :key="index" :label="item.doorName"
                         :value="item.doorName" v-for="(item, index) in doorBindNameOptions"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>


      <el-dialog :title="title1" :visible.sync="open1" append-to-body width="600px">
        <el-form ref="form1" :model="form1" :rules="rules1" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="小区" prop="areaName">
                <el-select v-model="form1.areaName" :style="{width: '100%'}" :filter-method="filterBindMethod" clearable
                           placeholder="请输入小区" size="small" @change="getGroupBind(form1.areaName)"
                           :disabled="true">
                  <el-option v-for="(item, index) in areaBindIdOptions" :key="index"
                             :label="item.areaName" :value="item.areaName"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="楼宇" prop="groupName">
                <el-select v-model="form1.groupName" :style="{width: '100%'}" clearable placeholder="请输入楼宇"
                           :disabled="true"
                           @change="getUnitBind(form1.areaName,form1.groupName)">
                  <el-option v-for="(item, index) in groupBindIdOptions" :key="index"
                             :label="item.groupName" :value="item.groupName"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="单元" prop="unitName">
                <el-select v-model="form1.unitName" :style="{width: '100%'}" clearable placeholder="请输入单元"
                           :disabled="true">
                  <el-option v-for="(item, index) in unitBindNameOptions" :key="index"
                             :label="item.unitName" :value="item.unitName"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="户" prop="doorName">
                <el-input v-model="form1.doorName" placeholder="请输入户" :disabled="true"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="userName">
                <el-input v-model="form1.userName" placeholder="请输入姓名"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电话" prop="tel">
                <el-input v-model="form1.areaBuild" placeholder="请输入电话号"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="证件号码" prop="papersCode">
                <el-input v-model="form1.papersCode" placeholder="请输入证件号码"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="面积" prop="space">
                <el-input v-model="form1.space" placeholder="请输入面积"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="用途" prop="purposeId">
                <el-select v-model="form1.purposeId" :style="{width: '100%'}" clearable
                           placeholder="请输入用途">
                  <el-option v-for="(item, index) in purposeOptions" :key="index" :disabled="item.disabled"
                             :label="item.purposeName" :value="item.purposeId"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="收费标准" prop="price">
                <el-select v-model="form1.price" :style="{width: '100%'}" clearable
                           placeholder="请输入收费标准">
                  <el-option v-for="(item, index) in feesOptions" :key="index" :disabled="item.disabled"
                             :label="item.price" :value="item.price"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="缴费金额" prop="costMoney">
                <el-input v-model="form1.costMoney" placeholder="请输入缴费金额"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="缴费时间" prop="costDate">
                <el-date-picker v-model="form1.costDate"
                                placeholder="选择缴费时间" :style="{width: '100%'}"
                                type="date" value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm1">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>



  </div>
</template>

<script>
import {
  getAreaInfo,
  getAreaInfo1,
  getDoorInfo,
  getGroupInfo,
  getUnitInfo,
  insertDoor,
  leftList,
  rightList,
  updateBind
} from "@/api/fund/doorBind";
import {
  getAreaBindInfo,
  getAreaBindInfo1,
  getBindInfo,
  getDoorBindInfo,
  getGroupBindInfo,
  getUnitBindInfo
} from "@/api/fund/doorBindQuery";
import {getFeesInfo, getPurposeInfo} from "@/api/house/door";


export default {
  name: "DoorBind",
  data() {
    return {
      // 遮罩层
      loading: true,
      loading1: true,
      // 选中数组
      ids: [],
      houseIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total1: 0,
      areaIdOptions: [{}],
      groupIdOptions: [{}],
      unitNameOptions: [{}],
      doorNameOptions: [{}],

      areaBindIdOptions: [{}],
      groupBindIdOptions: [{}],
      unitBindNameOptions: [{}],
      doorBindNameOptions: [{}],
      feesOptions: [{}],
      purposeOptions: [{}],
      // 户管理表格数据
      leftData: [],
      rightData: [],
      // 弹出层标题
      title: "",
      title1: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      form1: {},
      // 表单校验
      rules: {
        areaName: [
          {required: true, message: "小区不能为空", trigger: "change"}
        ],
        groupName: [
          {required: true, message: "楼宇不能为空", trigger: "change"}
        ],
        unitName: [
          {required: true, message: "单元不能为空", trigger: "change"}
        ],
        doorName: [
          {required: true, message: "户不能为空", trigger: "blur"}
        ]
      },
      rules1: {
        userName: [
          {required: true, message: "姓名不能为空", trigger: "change"}
        ],
        tel: [
          {required: true, message: "电话不能为空", trigger: "change"}
        ],
        papersCode: [
          {required: true, message: "证件号码不能为空", trigger: "change"}
        ],
        purposeName: [
          {required: true, message: "用途不能为空", trigger: "blur"}
        ],
        price: [
          {required: true, message: "收费标准不能为空", trigger: "blur"}
        ],
        space: [
          {required: true, message: "面积不能为空", trigger: "blur"}
        ],
        costMoney: [
          {required: true, message: "已缴金额不能为空", trigger: "blur"}
        ],
        costDate: [
          {required: true, message: "缴费时间不能为空", trigger: "change"}
        ]
      }
    };
  },
  created() {
    this.areaSelect();
    this.areaBindSelect();
    this.getLeftList();
    this.feesSelect();
    this.purposeSelect();
  },
  methods: {
    numberFormat(row, column, cellValue) {
      cellValue += ''
      if (!cellValue.includes('.')) cellValue += '.'
      return cellValue.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
        return $1 + ','
      }).replace(/\.$/, '')
    },
    /** 查询户管理列表 */
    getLeftList() {
      this.loading = true;
      leftList(this.queryParams).then(response => {
        this.leftData = response.rows;
        this.total = response.total;
        this.loading = false;
      });

    },

    getRightList() {
      this.loading1 = true;
      rightList(this.queryParams1).then(response => {
        this.rightData = response.rows;
        this.total1 = response.total;
        this.loading1 = false;
      });

    },
    filterMethod(val) {
      this.value = val
      getAreaInfo1(this.value).then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    areaSelect() {
      getAreaInfo().then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    getGroup(areaName) {
      if (areaName != "") {
        getGroupInfo(areaName).then(response => {
          this.groupIdOptions = response.rows;
        });
      }
    },
    getUnit(areaName, groupName) {
      getUnitInfo(areaName, groupName).then(response => {
        this.unitNameOptions = response.rows;
      });
    },
    getDoor(areaName, groupName, unitName) {
      getDoorInfo(areaName, groupName, unitName).then(response => {
        this.doorNameOptions = response.rows;
      });
    },
    purposeSelect() {
      getPurposeInfo().then(response => {
        this.purposeOptions = response.rows;
      });
    },
    feesSelect() {
      getFeesInfo().then(response => {
        this.feesOptions = response.rows;
      });
    },

    filterBindMethod(val) {
      this.value = val
      getAreaBindInfo1(this.value).then(response => {
        this.areaBindIdOptions = response.rows;
      });
    },
    areaBindSelect() {
      getAreaBindInfo().then(response => {
        this.areaBindIdOptions = response.rows;
      });
    },
    getGroupBind(areaName) {
      if (areaName != "") {
        getGroupBindInfo(areaName).then(response => {
          this.groupBindIdOptions = response.rows;
        });
      }
    },
    getUnitBind(areaName, groupName) {
      getUnitBindInfo(areaName, groupName).then(response => {
        this.unitBindNameOptions = response.rows;
      });
    },

    getDoorBind(areaName, groupName, unitName) {
      getDoorBindInfo(areaName, groupName, unitName).then(response => {
        this.doorBindNameOptions = response.rows;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.form1 = {};
      this.resetForm("form");
      this.resetForm("form1");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getLeftList();
    },
    handleQuery1() {
      this.getRightList();
    },


    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    resetQuery1() {
      this.resetForm("queryForm1");
      this.handleQuery1();
    },


    onSelectAll() {
      this.$refs.leftData.clearSelection();
    },
    selectItem(rows) {
      this.single = rows.length !== 1;
      if (rows.length == 1) {
        this.houseId = rows[0].houseId;
      }

      this.multiple = !rows.length;
      if (rows.length > 1) {
        const newRows = rows.filter((it, index) => {
          if (index == rows.length - 1) {
            this.$refs.leftData.toggleRowSelection(it, true);
            return true;
          } else {
            this.$refs.leftData.toggleRowSelection(it, false);
            return false;
          }
        });
        this.multipleSelection = newRows;
      } else {
        this.multipleSelection = rows;
      }
      this.userId = this.multipleSelection.length
        ? this.multipleSelection[0].guid
        : "";
    },

    onSelectOp(row) {
      this.$refs.leftData.clearSelection();
      this.$refs.leftData.toggleRowSelection(row, true);
      this.multipleSelection = [];
      this.multipleSelection.push(row);
    },


    updateBind() {
      this.reset();
      getBindInfo(this.houseId).then(response => {
        this.form1 = response.data;
        this.form1.houseId = this.houseId;
        this.open1 = true;
        this.title1 = "户绑定";
      });
    },

    submitForm1() {
      debugger
      this.$refs["form1"].validate(valid => {
        if (valid) {

          updateBind(this.form1).then(response => {
            this.$modal.msgSuccess("绑定成功");
            this.open1 = false;
            this.getLeftList()
          });
        }
      });
    },

    insertBind(row) {
      this.reset();
      this.open = true;
      this.title = "户新增";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          insertDoor(this.form).then(response => {
            this.$modal.msgSuccess("添加成功");
            this.open = false;
            this.getLeftList()
          });
        }
      });
    },


  }
};
</script>

<style lang='postcss'>
.left-bar {
  float: left;
  width: 50%;
  height: 100%;
  position: absolute;
  overflow: auto;
  box-shadow: 1px 1px #888888;

}

.right-bar {
  float: right;
  left: 53%;
  width: 47%;
  height: 100%;
  position: absolute;
  overflow: auto;
}

</style>

